package com.mmx.task.node;

import cn.hutool.core.collection.ListUtil;
import com.mmx.constant.NodeConstant;
import com.mmx.domain.bean.FlowData;
import com.mmx.domain.bean.LineData;
import com.mmx.domain.bean.NodeData;
import com.mmx.task.AbstractTaskNode;
import com.mmx.util.SpringUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @desc: 开始任务节点
 * @author: liushuai
 * @date: 2023/1/31 15:20
 */
@Service(NodeConstant.START)
public class StartTaskNode extends AbstractTaskNode {



    @Override
    public List<NodeData> execute(FlowData flowObj, NodeData currentNode, Map<String, Object> variables) {
        List<NodeData> targerNode = this.getTargerNode(flowObj, currentNode);
        if(targerNode.size() == 0){
            throw new RuntimeException("The start node has no line connection");
        }
        if(targerNode.size() > 1){
            throw new RuntimeException("The start node can connect to only one node");
        }
        NodeData nodeData = targerNode.get(0);
        if(!TRANSIT_NODE.contains(nodeData.getType())){
             return ListUtil.list(false,nodeData);
        }
        AbstractTaskNode taskNode = SpringUtil.getBean(nodeData.getType());
        List<NodeData> nodes = taskNode.execute(flowObj, nodeData,variables);
        return nodes;
    }

}
